
#include "tp2.h"

#define MIN(x,y) ( x < y ? x : y )
#define MAX(x,y) ( x > y ? x : y )

#define P 2

void mblur_c    (
    unsigned char *src,
    unsigned char *dst,
    int cols,
    int filas,
    int src_row_size,
    int dst_row_size)
{
	unsigned char (*src_matrix)[src_row_size] = (unsigned char (*)[src_row_size]) src;
	unsigned char (*dst_matrix)[dst_row_size] = (unsigned char (*)[dst_row_size]) dst;
	for (int i = 0; i < filas; i++){
		for (int j = 0; j < cols; j++){
			bgra_t *p_d = (bgra_t*)&dst_matrix[i][j*4];
			bgra_t *p_s = (bgra_t*)&src_matrix[i][j*4];
			p_d->a = p_s->a;
			if((i<2)||(j<2)||(i+2>=filas)||(j+2>=cols)){
				p_d->r = 0;
				p_d->g = 0;
				p_d->b = 0;
			}
			else{
				float rojo = 0;
				float verde = 0;
				float azul = 0;
				for(int i_aux = i-2,j_aux = j-2;i_aux<=i+2 && j_aux<=j+2;i_aux++,j_aux++){
					bgra_t* p_aux = (bgra_t*) &src_matrix[i_aux][j_aux*4];
					rojo+= p_aux->r;
					verde+= p_aux->g;
					azul+= p_aux->b;
				}
				rojo*=0.2;
				azul*=0.2;
				verde*=0.2;
				p_d->r = MIN(255,(unsigned char)rojo);
				p_d->b = MIN(255,(unsigned char)azul);
				p_d->g = MIN(255,(unsigned char)verde);	
			}
		}
	} 
}


